import time
from datetime import date

import pyautogui as pg
import undetected_chromedriver as uc
from faker import Faker

from google_account_create.do_with_element import DoWithElement
from google_account_create.log import log
from google_account_create.register import GOOGLE_URL

ENG_KEYS = r"QWERTYUIOP{}|ASDFGHJKL:" + '"' + r"ZXCVBNM<>?qwertyuiop[]asdfghjkl;'zxcvbnm,./"
RU_KEYS = r"ЙЦУКЕНГШЩЗХЪ/ФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,йцукенгшщзхъфывапролджэячсмитьбю."
DICT_KEYS: dict[str, str] = dict([(key, value) for key, value in zip(RU_KEYS, ENG_KEYS)])
SMALL = 450


def change_letter(word: str):
    global DICT_KEYS
    return "".join([DICT_KEYS.get(letter) if letter in RU_KEYS else letter for letter in word])


def check_mode_and_run(driver: uc.Chrome):
    try:
        width = DoWithElement("div[role='presentation']", driver).do().element.size.get("width")
    except:
        width = DoWithElement("div[jsaction*='CLIENT']", driver).do().element.size.get("width")

    if width < SMALL + 60:
        do_for_small(driver)
    else:
        do_for_big(driver)
    # log.info(f"width client: {width}")


def do_for_small(driver: uc.Chrome):
    _fk = Faker("ru_RU")
    first_name = _fk.first_name()
    last_name = _fk.last_name()
    date_of_birth: date = _fk.date_of_birth()
    day_of_birth = date_of_birth.day
    month_of_birth = date_of_birth.month
    year_of_birth = date_of_birth.year
    gender = _fk.passport_gender()
    gender_index = [1, 2][0 if gender == 'F' else 1]
    password = _fk.password()
    log.info(f"{first_name=} {password=} {month_of_birth=} {year_of_birth=} {gender_index=} {date_of_birth=}")

    # Создать аккаунт
    # Position: Point(x=714, y=777)
    pg.moveTo(714, 777, 0.6)
    pg.click()
    # Point(x=736, y=816) для личного использования
    pg.moveTo(736, 816, 0.4)
    pg.click()

    # Меняем раскладку
    pg.hotkey("optionleft", "space")
    # Point(x=953, y=554) имя
    pg.moveTo(953, 554, 0.3)
    pg.click()
    pg.typewrite(change_letter(first_name), 0.2)
    # Position: Point(x=959, y=648) Фамилия
    pg.moveTo(959, 648, 0.3)
    pg.click()
    pg.typewrite(change_letter(last_name), 0.2)
    pg.hotkey("optionleft", "space")
    # Далее
    pg.moveTo(990, 719)
    pg.click()
    pg.sleep(1)

    # День рождения узкий экран
    # Point(x=733, y=553)
    pg.moveTo(733, 553, 0.3)
    pg.click()
    pg.typewrite(str(day_of_birth))

    # Выбираем пол узкий экран
    pg.moveTo(959, 624, 0.3)
    pg.click()
    # узкий экран
    # Point(x=852, y=622, 619) жен, муж
    pg.moveTo(854, 624 + (gender_index * 2 + 1), 0.3)
    pg.click()

    # Мксяц рождения
    pg.moveTo(867, 548, 0.3)
    pg.click()
    pg.moveTo(867, 548 + ((month_of_birth - 1) * 2 + 1))
    pg.click()

    # Год рождения
    pg.moveTo(989, 550, 0.3)
    pg.click()
    pg.typewrite(str(year_of_birth))

    # Далее
    pg.moveTo(1003, 733, 0.3)
    pg.click()
    pg.sleep(1)

    # Выбор емайла
    pg.moveTo(836, 534, 0.3)
    pg.click()


def do_for_big(driver: uc.Chrome):
    _fk = Faker("ru_RU")
    first_name = _fk.first_name()
    last_name = _fk.last_name()
    date_of_birth: date = _fk.date_of_birth()
    day_of_birth = date_of_birth.day
    month_of_birth = date_of_birth.month
    year_of_birth = date_of_birth.year
    gender = _fk.passport_gender()
    gender_index = [1, 2][0 if gender == 'F' else 1]
    password = _fk.password()
    log.info(f"{first_name=} {password=} {month_of_birth=} {year_of_birth=} {gender_index=} {date_of_birth=}")

    # Position: Point(x=1168, y=767) создать аккаунт
    pg.moveTo(1168, 767, 0.4)
    pg.click()
    # Position: Point(x=1174, y=806) для личного использования
    pg.moveTo(1174, 806, 0.6)
    pg.click()

    # Меняем раскладку
    pg.hotkey("optionleft", "space")
    # Имя
    # Point(x=1037, y=563) имя
    pg.moveTo(1037, 554, 0.3)
    pg.click()
    pg.typewrite(change_letter(first_name), 0.2)
    # Фамилия
    # Position: Point(x=1078, y=648) Фамилия
    pg.moveTo(1078, 648, 0.3)
    pg.click()
    pg.typewrite(change_letter(last_name), 0.2)
    pg.hotkey("optionleft", "space")

    # Кнопка далее
    pg.moveTo(1329, 739, 0.3)
    pg.click()
    pg.sleep(1)

    # День рождения широкий экран
    # Point(x=1019, y=562)
    pg.moveTo(1019, 562, 0.3)
    pg.click()
    pg.typewrite(str(day_of_birth))

    # широкий экран пол
    # Point(x=852, y=622, 619) жен, муж
    pg.moveTo(921, 622, 0.3)
    pg.click()

    pg.moveTo(921, 624 + ((gender_index - 1) * 20 + 1), 0.3)
    pg.click()

    # Год рождения
    pg.moveTo(1278, 547, 0.4)
    pg.click()
    pg.sleep(0.2)
    pg.typewrite(str(year_of_birth), 0.2)

    # Мксяц рождения
    pg.moveTo(1109, 551, 0.3)
    pg.click()
    pg.sleep(0.2)
    pg.moveTo(1109, 551 + ((month_of_birth - 1) * 20 + 1), 0.3)
    pg.click()

    # Далее
    pg.moveTo(1284, 751, 0.3)
    pg.click()
    pg.sleep(1)

    # Выбор емайла
    pg.moveTo(1013, 536, 0.3)
    pg.click()
    pg.sleep(1)

    # Далее
    pg.moveTo(1326, 753, 0.3)
    pg.click()
    pg.sleep(1)

    # Ввод пароля
    pg.moveTo(975, 541, 0.3)
    pg.click()
    pg.typewrite(password, 0.4)
    pg.moveTo(960, 641, 0.2)
    pg.click()
    pg.typewrite(password, 0.3)
    pg.sleep(1)
    # Далее
    # pg.moveTo(1309, 762, 0.3)
    # pg.click()
    # pg.sleep(1)


def main():
    size = pg.size()
    log.info(f"Size: {size}")
    driver = uc.Chrome()
    driver.implicitly_wait(0.2)
    driver.get(GOOGLE_URL)
    pg.moveTo(1649, 162, 1)
    time.sleep(0.1)
    pg.click()

    check_mode_and_run(driver)

    time.sleep(10)
    position = pg.position()
    log.info(f"Position: {position}")


def main_():
    pg.moveTo(1649, 162, 1)
    time.sleep(0.1)
    pg.click()
    # time.sleep(5)
    # Point(x=1649, y=162) логин кнопка
    time.sleep(2)
    # Point(x=972, y=557) логин инпут
    pg.moveTo(972, 557, 0.8)
    time.sleep(0.1)
    log.info(f"вводим логин {first_name}")
    # pg.typewrite("galkin", 0.2)
    # from selenium.webdriver.common import keys
    # keyboard.write(first_name, delay=0.2)
    # Point(x=1004, y=772) далее
    pg.moveTo(1004, 772, 0.8)
    time.sleep(0.1)
    # pg.click()
    # Point(x=1004, y=772) далее2
    pg.moveTo(1315, 741, 0.8)
    time.sleep(0.1)
    # pg.click()

    # Вводим фамилию в русской раскладке, pgautogui нажимает клавиши с английскими буквами

    # Если вариант экрана узкий
    # Position: Point(x=1005, y=718) далее
    pg.moveTo(1005, 718, 0.3)
    pg.click()
    # Если вариант экрана широкий
    pg.moveTo(1329, 739, 0.3)
    pg.click()

    pg.hotkey("optionleft", "space")

    # Год рождения широкий экран
    # Point(x=1287, y=541)
    pg.moveTo(1287, 541, 0.3)
    pg.doubleClick()
    pg.press("backspace")
    pg.typewrite(str(year_of_birth))
    # Год рождения узкий экран
    # Point(x=1287, y=541)
    pg.moveTo(960, 547, 0.3)
    pg.doubleClick()
    pg.press("backspace")
    pg.typewrite(str(year_of_birth))

    # Месяц рождения узкий экран
    # Position: Point(x=842, y=549)
    pg.moveTo(842, 549, 0.3)
    pg.click()


if __name__ == "__main__":
    main()
    while True:
        pass
